#!/bin/sh
DIR=`pwd`
REDIS_DIR=/usr/local/redis
REDIS_SERVER_CONF=/etc/redis/redis_6379.conf
REDIS_SENTINEL_CONF=/etc/redis/redis_26379.conf
REDIS_SERVER_CONF_BAK=/etc/redis/redis_6379.conf.bak
REDIS_SENTINEL_CONF_BAK=/etc/redis/redis_26379.conf.bak
echo "-----------------------------------install redis-------------------------------"
if [ -d $REDIS_DIR ]; then
    echo "redis has installed"
    cd $DIR
    # tar -vxf redis-4.0.9.tar.gz
    # cd redis-4.0.9/
    # 替换配置文件
    ## 备份原来的文件
    mv -f $REDIS_SERVER_CONF $REDIS_SERVER_CONF_BAK
    mv -f $REDIS_SENTINEL_CONF $REDIS_SENTINEL_CONF_BAK
    ## 拷贝新文件
    cp -f ./redis/redis.conf $REDIS_SERVER_CONF
    cp -f ./redis/sentinel.conf $REDIS_SENTINEL_CONF
    # 重启服务
    service redis-server stop
    service redis-server start
    service redis-sentinel stop
    service redis-sentinel start
    exit 3
fi
## 解压redis.tar.gz
cd $DIR
mkdir -p $REDIS_DIR
tar -vxf redis-4.0.9.tar.gz -C $REDIS_DIR --strip-components 1
## 编译redis
cd $REDIS_DIR
make
cd src
make install
## 制作redis-server服务
cd $REDIS_DIR
mkdir -p /etc/redis
cp ./service_redis_server /etc/init.d/redis-server
# cp ./redis.conf $REDIS_SERVER_CONF
cd $DIR
cp ./redis/redis.conf $REDIS_SERVER_CONF
chmod 777 $REDIS_SERVER_CONF
chmod 777 /etc/init.d/redis-server
chkconfig redis-server on
## 制作redis-sentinel服务
cd $REDIS_DIR
mkdir -p /etc/redis
cp ./service_redis_sentinel /etc/init.d/redis-sentinel
# cp ./sentinel.conf $REDIS_SENTINEL_CONF
cd $DIR
cp ./redis/sentinel.conf $REDIS_SENTINEL_CONF
chmod 777 $REDIS_SENTINEL_CONF
chmod 777 /etc/init.d/redis-sentinel
chkconfig redis-sentinel on
echo "-----------------------------------install redis success-------------------------------"
service redis-server start
service redis-sentinel start
cd $DIR